package com.boyuan.huijiaoyu;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.adapter.IntroAdapter;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.widget.viewpagerindicator.CirclePageIndicator;

public class IntroActivity extends BaseActivity implements OnClickListener {

	private final static String TAG = "IntroActivity";

	private ViewPager viewPager;
	private IntroAdapter adapter;
	private CirclePageIndicator circlePageIndicator;
	private Button btnSkip;
	private List<Integer> datas;
	private RelativeLayout layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.intro);

		prepareView();

	}

	private void prepareView() {

		// 更新本地数据，下次重新打开应用不显示功能介绍页;
		SharePreferenceHelper.setIntValue(getApplicationContext(),
				SharePreferenceHelper.INTRO, 1);

		viewPager = (ViewPager) findViewById(R.id.viewpager);
		circlePageIndicator = (CirclePageIndicator) findViewById(R.id.circlePageIndicator1);

		btnSkip = (Button) findViewById(R.id.btn_skip);
		btnSkip.setOnClickListener(this);
		layout = (RelativeLayout) findViewById(R.id.layout);
		datas = new ArrayList<Integer>();

		// datas.add(R.drawable.tutorial_background_01);
		// datas.add(R.drawable.tutorial_background_02);
		// datas.add(R.drawable.tutorial_background_03);

		adapter = new IntroAdapter(datas, getApplicationContext());
		viewPager.setAdapter(adapter);
		circlePageIndicator.setViewPager(viewPager);

		circlePageIndicator.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub

				Log.w(TAG, "index:" + arg0);

				if (arg0 == 2) {
					btnSkip.setVisibility(View.VISIBLE);
					layout.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent mainIntent = new Intent(IntroActivity.this,
									MainActivity.class);
							startActivity(mainIntent);
							finish();
						}
					});
				} else {
					btnSkip.setVisibility(View.GONE);
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	@Override
	public void backClicked() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_skip:

			Intent mainIntent = new Intent(IntroActivity.this,
					MainActivity.class);
			startActivity(mainIntent);
			finish();

			break;
		default:
			break;
		}

	}

	@Override
	public void onBackPressed() {
		// 不返回
		Log.e(TAG, "Don't go back");

	}

}
